package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.AreaBiz;
import com.egao.orderB.entity.Area;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-10-20 15:32:56
 * @version ：1.0
 */
@Repository
public class AreaBizImpl extends BaseDao implements AreaBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Area area, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Area> rm = new BeanPropertyRowMapper<Area>(Area.class);
		StringBuffer sql = new StringBuffer("select a.id,a.area_id,a.area");
        sql.append(",a.father,a.organization_id,a.create_time,a.create_user");
		sql.append(",a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_sys_area a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id ");
        sql.append(" where a.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(area)){
			if(Utils.isNotEmpty(area.getFather())){
				sql.append(" and instr(a.father,:father)>0 ");
				map.put("father", area.getFather());
			}
			if(Utils.isNotEmpty(area.getSort())&&Utils.isNotEmpty(area.getOrder())){
                sql.append(" order by a."+area.getSort()+" "+area.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Area> list(Area area, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Area> rm = new BeanPropertyRowMapper<Area>(Area.class);
        StringBuffer sql = new StringBuffer("select a.id,a.area_id,a.area");
        sql.append(",a.father,a.organization_id,a.create_time,a.create_user");
		sql.append(",a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_sys_area a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id ");
        sql.append(" where a.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(area)){
			if(Utils.isNotEmpty(area.getFather())){
				sql.append(" and instr(a.father,:father)>0 ");
				map.put("father", area.getFather());
			}
            if(Utils.isNotEmpty(area.getSort())&&Utils.isNotEmpty(area.getOrder())){
				sql.append(" order by a."+area.getSort()+" "+area.getOrder());
			} else {
				sql.append(" order by a.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Area getById(Area area) {
		RowMapper<Area> rm = new BeanPropertyRowMapper<Area>(Area.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select a.id,a.area_id,a.area");
        sql.append(",a.father,a.organization_id,a.create_time,a.create_user");
		sql.append(",a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_sys_area a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id ");
        sql.append(" where a.deleted=0 and sogzn.deleted=0 and a.id=:id ");
		map.put("id", area.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Area area, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_sys_area (");
        sql.append("area_id,area,father,organization_id");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":area_id,:area,:father,:organization_id");
		sql.append(",:create_user,now())");
        map.put("area_id", area.getAreaId());
        map.put("area", area.getArea());
        map.put("father", area.getFather());
        map.put("organization_id", loginUser.getOrganizationId() + "");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Area area, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_sys_area set ");
        sql.append("area_id=:area_id,area=:area,father=:father ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("area_id", area.getAreaId());
        map.put("area", area.getArea());
        map.put("father", area.getFather());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", area.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Area area, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(area.getId().length()>0){
			for(String ID : area.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_sys_area set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
